package com.xjh.t.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.DataGridView;
import com.xjh.t.common.ResultObj;
import com.xjh.t.domain.EnglishWord;
import com.xjh.t.domain.Sentence;
import com.xjh.t.service.ISentenceService;
import com.xjh.t.vo.EnglishWordVo;
import com.xjh.t.vo.SentenceVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author satela
 * @since 2020-10-27
 */
@RestController
@RequestMapping("/sentence")
public class SentenceController {

    @Autowired
    private ISentenceService sentenceService;

    @RequestMapping("/addSentence")
    public ResultObj addSentence(Sentence sentence) {

        QueryWrapper<Sentence> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("eng_sentence",sentence.getEngSentence());

        List<Sentence> list = this.sentenceService.list(queryWrapper);

        if(list.size() > 0)
        {
            return ResultObj.ADD_ERROR;
        }

        this.sentenceService.save(sentence);

        return ResultObj.ADD_SUCESS;

    }

    @RequestMapping("/updateSentence")
    public ResultObj updateWord(Sentence sentence) {

        this.sentenceService.updateById(sentence);

        return ResultObj.UPDATE_SUCESS;

    }

    @RequestMapping("/loadAllSentence")
    public DataGridView loadAllSentence(SentenceVo sentenceVo) {

        IPage<Sentence> page=new Page<>(sentenceVo.getPage(), sentenceVo.getLimit());
        QueryWrapper<Sentence> queryWrapper=new QueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(sentenceVo.getEngSentence()), "eng_sentence",sentenceVo.getEngSentence());
        queryWrapper.like(StringUtils.isNotBlank(sentenceVo.getChSentence()), "ch_sentence",sentenceVo.getChSentence());


        this.sentenceService.page(page, queryWrapper);

        return new DataGridView(page.getTotal(),page.getRecords());

    }
    @RequestMapping("/searchSentence")
    public List<Sentence> searchSentence(String keyword) {


        QueryWrapper<Sentence> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(keyword),"eng_sentence",keyword);

        List<Sentence> list = this.sentenceService.list(queryWrapper);

        return list;

    }


}

